package com.usian.behavior.service.impl;

import com.usian.behavior.mapper.ApBehaviorEntryMapper;
import com.usian.behavior.mapper.ApFollowBehaviorMapper;
import com.usian.behavior.service.ApBehaviorEntryService;
import com.usian.behavior.service.ApFollowBehaviorService;
import com.usian.model.behavior.dtos.FollowBehaviorDto;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.behavior.pojos.ApFollowBehavior;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApFollowBehaviorServiceImpl implements ApFollowBehaviorService {
    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    private ApFollowBehaviorMapper apFollowBehaviorMapper;


    @Override
    public ResponseResult saveFollowBehavior(FollowBehaviorDto dto) {
        //1：验证参数
       if(dto==null){
           return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
       }

       //2:得到父 entryid
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByEquOrUserId(dto.getUserId(), 1);

       if(apBehaviorEntry==null){
           return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"关注行为实体表数据不存在");
       }

        //保存关注行为
        ApFollowBehavior alb = new ApFollowBehavior();
        alb.setEntryId(apBehaviorEntry.getId());
        alb.setCreatedTime(new Date());
        alb.setArticleId(dto.getArticleId());
        alb.setFollowId(dto.getFollowId());
        apFollowBehaviorMapper.insert(alb);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
